package com.ruoyi.contact.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contact.domain.ContactCategory;
import com.ruoyi.contact.service.IContactCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 联系人类别Controller
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Controller
@RequestMapping("/contact/category")
public class ContactCategoryController extends BaseController
{
    private String prefix = "contact/category";

    @Autowired
    private IContactCategoryService contactCategoryService;

    @RequiresPermissions("contact:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询联系人类别列表
     */
    @RequiresPermissions("contact:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ContactCategory contactCategory)
    {
        startPage();
        List<ContactCategory> list = contactCategoryService.selectContactCategoryList(contactCategory);
        return getDataTable(list);
    }

    /**
     * 导出联系人类别列表
     */
    @RequiresPermissions("contact:category:export")
    @Log(title = "联系人类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ContactCategory contactCategory)
    {
        List<ContactCategory> list = contactCategoryService.selectContactCategoryList(contactCategory);
        ExcelUtil<ContactCategory> util = new ExcelUtil<ContactCategory>(ContactCategory.class);
        return util.exportExcel(list, "联系人类别数据");
    }

    /**
     * 新增联系人类别
     */
    @RequiresPermissions("contact:category:add")
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        modelMap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/add";
    }

    /**
     * 新增保存联系人类别
     */
    @RequiresPermissions("contact:category:add")
    @Log(title = "联系人类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ContactCategory contactCategory)
    {
        contactCategory.setUserId(ShiroUtils.getUserId());
        return toAjax(contactCategoryService.insertContactCategory(contactCategory));
    }

    /**
     * 修改联系人类别
     */
    @RequiresPermissions("contact:category:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        ContactCategory contactCategory = contactCategoryService.selectContactCategoryByCategoryId(categoryId);
        mmap.put("contactCategory", contactCategory);
        mmap.put("userString",ShiroUtils.getSysUser().toUserString());
        return prefix + "/edit";
    }

    /**
     * 修改保存联系人类别
     */
    @RequiresPermissions("contact:category:edit")
    @Log(title = "联系人类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ContactCategory contactCategory)
    {
        contactCategory.setUserId(ShiroUtils.getUserId());
        return toAjax(contactCategoryService.updateContactCategory(contactCategory));
    }

    /**
     * 删除联系人类别
     */
    @RequiresPermissions("contact:category:remove")
    @Log(title = "联系人类别", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contactCategoryService.deleteContactCategoryByCategoryIds(ids));
    }
}
